package com.heima.article.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.article.service.CollectionBehaviorService;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.pojos.Behavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class CollectionBehaviorServiceImpl implements CollectionBehaviorService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 文章收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {

        Boolean flag =true;


        //        1.校验参数
        if (dto == null || dto.getType() == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getOperation()==1){
            flag=false;
        }

//        2。设置参数
        Behavior behavior = new Behavior();
        behavior.setArticleId(dto.getEntryId());
        behavior.setIscollection(flag);

//        3.redis

//        获取用户id
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userid = user.getId();
        String uid = userid.toString();
        if(userid==0 || userid==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"请登录后访问");
        }
//        4判断用户行为
//        取出用户JSON将用户行为的JSON转Map
        String mapJSon = (String) redisTemplate.opsForValue().get(uid);
        Map articlemap = JSON.parseObject(mapJSon, Map.class);

        if (articlemap == null || articlemap.size() == 0) {
//            为空添加
            articlemap = new HashMap();
            SaveBehavior(dto.getEntryId(), behavior, uid, articlemap);
        }

        String be = (String) articlemap.get(dto.getEntryId());
        Behavior behavior1 = JSON.parseObject(be, Behavior.class);

        if (behavior1 == null ) {
            behavior.setIscollection(flag);
            SaveBehavior(dto.getEntryId(), behavior, uid, articlemap);
        } else if (behavior1.getIscollection()==null){
            behavior1.setIscollection(flag);
            SaveBehavior(dto.getEntryId(), behavior1, uid, articlemap);
        }else {
            if (flag){
                behavior1.setIscollection(null);
            }
            behavior1.setIscollection(flag);
            SaveBehavior(dto.getEntryId(), behavior1, uid, articlemap);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加behavior到redis
     *
     * @param id
     * @param behavior
     * @param userid
     * @param articlemap
     */
    private void SaveBehavior(Long id, Behavior behavior, String userid, Map articlemap) {
        String behaviorJSON = JSON.toJSONString(behavior);
        articlemap.put(id, behaviorJSON);
        String key = userid.toString();
        String value = JSON.toJSONString(articlemap);
        redisTemplate.opsForValue().set(key, value);
    }
}
